package com.aim.analysis.records;

import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.stat.descriptive.rank.Median;

import java.util.Arrays;

/**
 * User: Avraham Shvartzon
 * Date: 2/5/15
 */
public class MedianPredictor implements PerformancePredictor<MedianPredictor.Params> {

    private Params p;
    private Median median = new Median();

    @Override
    public double[] predict(double[] performance) {
        double[] predict = new double[performance.length];
        predict[0] = p.initialDelay;
        for (int i = 1; i < performance.length; i++) {
            int start = Math.max(0, i - p.backSize);
            int length = i - start;
            double[] subArray = Arrays.copyOfRange(performance, start, start + length);
            Arrays.sort(subArray);
            predict[i] = median.evaluate(subArray, 0, subArray.length);
        }
        return predict;
    }

    @Override
    public void setParameters(Params parameters) {
        this.p = parameters;
    }

    public static class Params{

        private int backSize;
        private int initialDelay;

        public Params(int initialDelay, int backSize) {
            this.initialDelay = initialDelay;
            this.backSize = backSize;
        }

        @Override
        public String toString() {
            return "backSize=" + backSize +", initialDelay=" + initialDelay;
        }
    }
}
